<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;

use app\api\service\Token;
use app\lib\enum\MessageDetailEnum;
use app\lib\enum\PageEnum;

/**
 * 消息类别
 * Class Message
 * @package app\api\model
 */
class Message extends BaseModel
{
    /**
     * @Desc 格式化日期
     *
     * @param $value
     *
     * @return false|string
     */
    protected function getCreateTimeAttr($value)
    {
        return date('Y-m-d', $value);
    }

    /**
     * @Desc 为图片添加URL前缀
     *
     * @param $value
     *
     * @return string
     */
    protected function getImageAttr($value)
    {
        return $this->addWebUrlPrefixToImage($value);
    }

    /**
     * @Desc 查询消息类别
     * @return array
     */
    public static function getMessageList()
    {
        $id = Token::getUidFromCache();
        $list = (new self)->field('id, title, image')->order('id asc')->select()->toArray();
        foreach ($list as $key => $value) {
            $info = MessageDetail::field('status,info,create_time')->where(['message_id' => $value['id'], 'user_weixin_id' => $id])->order('create_time desc')->find();
            if (!empty($info)) {
                $list[$key]['status'] = $info['status'];
                $list[$key]['message'] = mb_strlen($info['info']) < 11 ? $info['info'] : mb_substr($info['info'], 0, 11) . '...';
                $list[$key]['create_time'] = $info['create_time'];
            } else {
                $list[$key]['create_time'] = date('Y-m-d');
                $list[$key]['status'] = 1;
                $list[$key]['message'] = '暂无消息';
            }
        }
        return $list;
    }

    /**
     * @Desc 根据订单状态查询订单
     *
     * @param int $status
     * @param int $page
     *
     * @return array
     */
    public static function getMessageDetailList($status, $page = 1)
    {
        $id = Token::getUidFromCache();
        // 分页数量
        $listRows = PageEnum::SYSMSG;
        $list = MessageDetail::field('id, title, create_time, info')->where(['user_weixin_id' => $id, 'message_id' => $status])->page($page, $listRows)->order('create_time desc')->select()->toArray();
        return $list;
    }

    /**
     * @Desc 改变消息状态
     * 消息类别id
     *
     * @param int $id
     *
     * @return bool
     */
    public static function updateMessageDetail($id)
    {
        $uid = Token::getUidFromCache();
        // 未读状态改为已读
        MessageDetail::update(['status' => MessageDetailEnum::YLOOK], ['user_weixin_id' => $uid, 'message_id' => $id]);
        //        if (!$result) {
        //            throw new CurdException([
        //                'message'   => '取消订单失败',
        //                'errorCode' => 60001,
        //            ]);
        //        }
        return true;
    }

    /**
     * @Desc 获取未读消息个数
     * @return int|string
     */
    public static function getMessageNumber()
    {
        $id = Token::getUidFromCache();
        return (new MessageDetail())->where(static function ($query) use ($id) {
            $query->where('user_weixin_id', '=', $id)->whereOr('user_weixin_id', '=', 0);
        })->where('status', '=', 0)->count();
    }
}